package repository

import (
	"errors"
	"gin-shop-admin/common"
	"gin-shop-admin/dao"
	"gin-shop-admin/models"
	"time"
)

func InsertOneManagerr(manager *models.Manager) error {
	manager.Password = common.Sha1(manager.Password)
	return dao.DB.Create(&manager).Error
}

func GetAllManagers() (users []*models.Manager, err error) {
	if err = dao.DB.Find(&users).Error; err != nil {
		return nil, err
	}
	return
}

type ManagerRes struct {
	Id       int       `gorm:"column:mg_id;" json:"id" form:"id"`
	Username string    `gorm:"column:mg_name" json:"username" form:"username"`
	Password string    `gorm:"column:mg_pwd;" json:"password" form:"password"`
	Create_time  time.Time	`grom:"column:create_time" json:"create_time" form:"create_time"`
	Update_time time.Time	`grom:"column:update_time" json:"update_time" form:"update_time"`
	RoleName string    `gorm:"column:role_name" json:"role_name" form:"role_name"`
	Mobile   string    `gorm:"column:mg_mobile" json:"mobile" form:"mobile"`
	Email    string    `gorm:"column:mg_email" json:"email" form:"email"`
	State    bool      `gorm:"column:mg_state" json:"state" form:"state"`
}

func GetManagers(query string, PageNum int, PageSize int) (total int, managers []*ManagerRes, err error) {
	if err = dao.DB.Model(&models.Manager{}).Count(&total).Error; err != nil {
		return 0, nil, err
	}
	var query_Sql string
	if query == "" {
		query_Sql = "select mg_id, mg_name, mg_pwd, create_time, update_time, role_name, mg_mobile, mg_email, mg_state from sp_manager " +
			"left join sp_role on sp_manager.role_id=sp_role.role_id " +
			"limit ? offset ?"
	} else {
		query_Sql = "select mg_id, mg_name, mg_pwd, create_time, update_time, role_name, mg_mobile, mg_email, mg_state from sp_manager " +
			"left join sp_role on sp_manager.role_id=sp_role.role_id " +
			"where CONCAT(IFNULL(sp_manager.mg_name, ''), IFNULL(sp_manager.mg_email,''),IFNULL(sp_role.role_name,''),IFNULL(sp_manager.mg_mobile,'')) " +
			"like '%" + query + "%' " +
			"limit ? offset ?"
	}
	dao.DB.Raw(query_Sql, PageSize, (PageNum-1)*PageSize).Scan(&managers)
	if len(managers) != 0 {
		return total, managers, nil
	}
	return total, nil, errors.New("查询结果为空")
}

func GetManagerByID(id string) (manager *models.Manager, err error) {
	manager = new(models.Manager)
	if err = dao.DB.Where("mg_id=?", id).First(&manager).Error; err != nil {
		return nil, err
	}
	return
}

func UpdateOneManager(manager *models.Manager) (error) {
	return dao.DB.Save(manager).Error
}

func DeleteManagerByID(id string) (error) {
	return dao.DB.Where("mg_id=?", id).Delete(&models.Manager{}).Error
}

func CheckExistManager(username string) (manager models.Manager, ok bool) {
	dao.DB.Where(models.Manager{Username: username}).First(&manager)
	if manager.Id > 0 {
		return manager, true
	}
	return
}

func ValidePassword(manager_pwd string, get_password string) bool {
	pwd := common.Sha1(get_password)
	if pwd == manager_pwd {
		return true
	}
	return false
}

func LoginCheckManger(username string, password string) (manager models.Manager, msg string, isPass bool) {
	manager, ok := CheckExistManager(username)
	if !ok {
		return manager, "用户不存在", false
	} else {
		if !ValidePassword(manager.Password, password) {
			return manager, "密码错误", false
		} else {
			return manager, "登录成功", true
		}
	}
}

func UpdateManagerState(uid string, state_str string) error {
	var state int
	if state_str == "true" {
		state = 1
	} else if state_str == "false" {
		state = 0
	} else {
		return errors.New("state参数类型有误")
	}
	return dao.DB.Model(&models.Manager{}).Where("mg_id=?", uid).Update("mg_state", state).Error
}

func UpdateManagerRole(uid int, roleID int) (err error) {
	return dao.DB.Model(models.Manager{}).Where(&models.Manager{Id:uid}).Update(&models.Manager{RoleId:roleID}).Error
}